package com.ruoyi.web.wapper;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.wapper.BaseControllerWrapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class CountInfoAllWapper extends BaseControllerWrapper {

    public CountInfoAllWapper(Map<String, Object> single) {
        super(single);
    }

    public CountInfoAllWapper(List<Map<String, Object>> multi) {
        super(multi);
    }

    @Override
    public void wrapTheMap(Map<String, Object> map) {

        try {

            Integer uvCount = (Integer) map.get("uvCount");
            BigDecimal uvCost = (BigDecimal) map.get("uvCost");

            BigDecimal channelRate = (BigDecimal) map.get("channelRate");

            Integer registerCount = (Integer) map.get("registerCount");
            BigDecimal registerCost = (BigDecimal) map.get("registerCost");

            BigDecimal registerCostOff = registerCost;
            if (ObjectUtil.isNotEmpty(channelRate)){
                registerCostOff = channelRate.multiply(registerCost);
                map.put("registerCountOff", channelRate.multiply(new BigDecimal(registerCount)));
                map.put("registerCostOff", registerCostOff);
            }

            map.put("registerRate", countRate(new BigDecimal(registerCount), new BigDecimal(uvCount)));

            Integer authCount = (Integer) map.get("authCount");
            map.put("authRate", countRate(new BigDecimal(authCount), new BigDecimal(registerCount)));

            Integer downloadCount = (Integer) map.get("downloadCount");
            map.put("downloadRate", countRate(new BigDecimal(downloadCount), new BigDecimal(registerCount)));


            BigDecimal proUvProfit = (BigDecimal) map.get("proUvProfit");
            BigDecimal proUnionProfit = (BigDecimal) map.get("proUnionProfit");

            BigDecimal cost = uvCost.add(registerCostOff);
            BigDecimal profit = proUvProfit.add(proUnionProfit);
            map.put("roiRate", countRate(profit, cost));

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private String countRate(BigDecimal one, BigDecimal two){

        if (ObjectUtil.isEmpty(two) || two.compareTo(BigDecimal.ZERO) ==0){
            return "0%";
        }
        return one.divide(two, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2) + "%";
    }
}
